const { getUserInfo, getUserInfoWidthCredentials } = require('../../common/utils/ws/wsxcx-opendata-sdk');
const app = getApp();
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    isAdmin: null,
  },
  collectformid(e) {
    let formid = e.detail.formId
    let openid = wx.getStorageSync('openid') || wx.getStorageSync('wsOpenID') || wx.getStorageSync('OPENID')
    app.util.request({
      url: 'pushService/api/formid',
      data: {
        formid,
        openid
      },
      method: 'GET'
    })
    this.checkUserInfo();
  },
  onShow(){
    this.getBaseInfo();
  },  
  onLoad(options) {
    this.checkUserInfo();
    this.setData({
      isAdmin: JSON.parse(options.isAdmin),
    });
  },
  getUserInfoWidthCredentials(e) {
    getUserInfoWidthCredentials({
      url: 'auth/session/userinfo',
      method: 'POST',
      detail: e.detail,
      cachetime: 0,
    }).then(res => {
      getUserInfo().then(res => {
        this.setData({
          userInfo: wx.getStorageSync('wsUserInfo'),
          hasUserInfo: true,
        });
      });
    });
  },
  checkUserInfo() {
    getUserInfo().then(res => {
      this.setData({
        userInfo: wx.getStorageSync('wsUserInfo'),
        hasUserInfo: true,
      });
    }).catch(res => {
      this.setData({
        hasUserInfo: false,
      });
    });
  },
  toPage(e) {
    let { url } = e.currentTarget.dataset;
    let { isAdmin } = this.data;
    url = `${url}?isAdmin=${isAdmin}`;
    const { hasUserInfo } = this.data;
    if (!hasUserInfo) {
      wx.showModal({
        title: '温馨提示',
        content: '点击登录体验更多功能',
      });
      return;
    }
    wx.navigateTo({ url });
  },
  getBaseInfo(){
    let { recept_person = '', control_title = '' } = wx.getStorageSync('alarmBaseInfo');
    this.setData({
      recept_person,
      control_title,
    });
  },
});

